package edu.lingnan.rili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.lingnan.rili.controller.MarksController;
import edu.lingnan.rili.mapper.MatchMapper;
import edu.lingnan.rili.pojo.Marks;
import edu.lingnan.rili.mapper.MarksMapper;
import edu.lingnan.rili.pojo.Match;
import edu.lingnan.rili.service.MarksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.lingnan.rili.vo.MarksVo;
import edu.lingnan.rili.vo.PagingQueryResult;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈世杰
 * @since 2022-01-23
 */
@Service
public class MarksServiceImpl extends ServiceImpl<MarksMapper, Marks> implements MarksService {
    @Autowired
    private MarksMapper marksMapper;

    @Autowired
    private MatchMapper matchMapper;

    @Override
    public List<Marks> selectByPositiveAndAnnounced() {
        //创建一个QueryWrapper的对象
        QueryWrapper<Marks> wrapper = new QueryWrapper<>();

        // 查询match之外的字段
        wrapper.select(Marks.class, item-> !"match".equals(item.getColumn()));

        //通过QueryWrapper设置条件
        wrapper.ge("add_score" , 0);
        List<Marks> marksList = marksMapper.selectList(wrapper);
        for (Marks marks : marksList) {
            Match match = matchMapper.selectById(marks.getUsersId());
            // marks.setMatch(match);
        }
        return marksList;
    }

    @Override
    public List<MarksVo> selectByAnnounced() {
        //创建一个QueryWrapper的对象
        QueryWrapper<Marks> marksWrapper = new QueryWrapper<>();

        // 查询match之外的字段
        marksWrapper.select(Marks.class, item-> !"match".equals(item.getColumn()));

        //通过QueryWrapper设置条件, 查询加分记录
        marksWrapper.ge("add_score" , 0);
        List<Marks> marksList = marksMapper.selectList(marksWrapper);
        ArrayList<MarksVo> marksVos = new ArrayList<>();

        if (!ObjectUtils.isEmpty(marksList)) {
            for (Marks marks : marksList) {
                MarksVo marksVo = new MarksVo();
                // 将 marks 中相同属性名的数据 copy 到marksVo 中
                BeanUtils.copyProperties(marks, marksVo);

                // 设置查询条件
                QueryWrapper<Match> matchWrapper = new QueryWrapper<>();
                matchWrapper.eq("users_id", marks.getUsersId());

                // 对于marks对象和marksVo对象不同的属性需要手动设置
                Match match = matchMapper.selectOne(matchWrapper);
                BeanUtils.copyProperties(match, marksVo);
                marksVos.add(marksVo);
            }
        }
        return marksVos;
    }

    @Override
    public PagingQueryResult<MarksVo> selectByAnnouncedAndPage(int currentPage, int pageSize) {
        // 创建一个QueryWrapper的对象
        QueryWrapper<Marks> marksWrapper = new QueryWrapper<>();

        // 查询match之外的字段
        marksWrapper.select(Marks.class, item-> !"match".equals(item.getColumn()));

        //通过QueryWrapper设置条件, 查询加分记录
        marksWrapper.ge("add_score" , 0);


        Page<Marks> marksPage = marksMapper.selectPage(new Page<>(currentPage, pageSize), marksWrapper);

        // 获取查询结果数据
        List<Marks> marksList = marksPage.getRecords();
        // 设置查询对象属性
        PagingQueryResult<MarksVo> pageResult = new PagingQueryResult<>();
        pageResult.setPageTotal((int) marksPage.getTotal());
        pageResult.setRows(marksList.size());

        ArrayList<MarksVo> marksVos = new ArrayList<>();

        if (!ObjectUtils.isEmpty(marksList)) {
            for (Marks marks : marksList) {
                MarksVo marksVo = new MarksVo();
                // 将 marks 中相同属性名的数据 copy 到marksVo 中
                BeanUtils.copyProperties(marks, marksVo);

                // 设置查询条件
                QueryWrapper<Match> matchWrapper = new QueryWrapper<>();
                matchWrapper.eq("users_id", marks.getUsersId());

                // 对于marks对象和marksVo对象不同的属性需要手动设置
                Match match = matchMapper.selectOne(matchWrapper);
                BeanUtils.copyProperties(match, marksVo);
                marksVos.add(marksVo);
            }
            pageResult.setDataList(marksVos);
        }
        return pageResult;
    }

    @Override
    public Boolean addData(MarksVo marksVo, Integer fractionFlag) {
        // 判断学生信息是否错误
        Match match = matchMapper.selectById(marksVo.getUsersId());
        if (match == null) {
            return false;
        }
        // 创建 marks 其他比赛加分对象，并赋予数据
        Marks marks = new Marks();
        BeanUtils.copyProperties(marksVo, marks);
        marks.setAddTime(LocalDateTime.now());
        // 设置未公开
        marks.setPubblicityType(Marks.NOT_ANNOUNCED);
        // 判断加减分
        if (fractionFlag.equals(MarksController.DEDUCT__SCORE)) {
            marks.setAddScore(marks.getAddScore() * -1);
        }
        System.out.println(marks);
        int row = marksMapper.insert(marks);
        return row > 0;
    }
}
